package controller.core;

import service.CoreService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

@WebServlet(name = "EndServlet", value = "/EndServlet")
public class EndServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String floorName = request.getParameter("floorName");
        int number = Integer.parseInt(request.getParameter("number"));
        Timestamp reserveTime = Timestamp.valueOf(request.getParameter("reserveTime"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(reserveTime);

        // end the seat(floorName,number,reserveTime)
        Timestamp startTime = new CoreService().endUsing(floorName,number,time);
        request.setAttribute("startTime", startTime);

        request.getRequestDispatcher("/RGetRecordController").forward(request, response);
    }
}
